package com.atguigu.srb.core.controller.api;


import com.atguigu.common.result.R;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.pojo.vo.UserBindVo;
import com.atguigu.srb.core.service.UserBindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author 杜佳佳
 * @since 2021-04-13
 */
@Api(tags = "用户绑定管理")
@RestController
@RequestMapping("/api/core/userBind")
public class ApiUserBindController {

    @Autowired
    private UserBindService userBindService;

    @ApiOperation("账户绑定提交数据")
    @PostMapping("/auth/bind")
    public R bind(@RequestBody UserBindVo userBindVo, HttpServletRequest request){

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        String fromStr = userBindService.commitBindUser(userBindVo,userId);

        return R.ok().data("fromStr",fromStr);
    }

    @ApiOperation("账户返回异步回调")
    @PostMapping("/notify")
    public String notify1(HttpServletRequest request) {

        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());

        if (!RequestHelper.isSignEquals(paramMap)){
            return "fail";
        }
        userBindService.notifys(paramMap);
        return "success";
    }


}

